package pl.edu.agh.neuraleconomy.persistence.exchange;

import java.sql.SQLException;

import pl.edu.agh.neuraleconomy.common.exception.ApplicationRuntimeException;
import pl.edu.agh.neuraleconomy.model.exchange.Company;
import pl.edu.agh.neuraleconomy.persistence.base.BaseDao;

public class CompanyDao extends BaseDao<Company> {

	public CompanyDao() {
		super(Company.class);
	}

	public Company getByName(String name) {
		try {
			return getFirst(dao.queryForEq("name", name));
		} catch (SQLException e) {
			logger.error("Nie udalo sie odczytac obiektu Company o nazwie " + name, e);
			throw new ApplicationRuntimeException(e);
		}
	}

	public Company getByNameOrCreate(String name) {
		Company company = getByName(name);
		if (company == null) {
			company = new Company();
			company.setName(name);
			create(company);
		}
		return company;
	}

}
